//
//  PersonInfoViewController.m
//  EducationAdminClient
//
//  Created by 孙添凤 on 16/9/23.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "PersonInfoViewController.h"
#import "Masonry.h"
#import "NetRequest.h"
#import "ListDetailCell.h"
#import "UILabel+StringFrame.h"

@interface PersonInfoViewController (){
    NSMutableDictionary *dic;
    NSMutableArray *dataArr;
}


@end

@implementation PersonInfoViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.cusTabView.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.cusTabView.userInteractionEnabled = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dic =[[NSMutableDictionary alloc]init];
    dataArr =[[NSMutableArray alloc]init];
    self.tbView =[[UITableView alloc]init];
    self.tbView.separatorStyle = NO;
    [self.view addSubview:self.tbView];
    self.tbView.delegate = self;
    self.tbView.dataSource = self;
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    [self createLeftNavBar];
    [self setTitleLabelText:@"个人资料"];
    [self dataRequest];
}

-(void)dataRequest{

    NSString *requestUrl;
    NSMutableDictionary *parama =[[NSMutableDictionary alloc]init];

    
    
    
    if ([getUIStyle() isEqualToString:STYLE0]) {
//        NSMutableDictionary *parmas = [[NSMutableDictionary alloc]init];
        NSDictionary *personDictionary = personDic();
        NSLog(@"%@",personDictionary);
        NSString *tableId = [NSString stringWithFormat:@"%@",[personDictionary objectForKey:@"tableId"]];
        NSString *pageId = [NSString stringWithFormat:@"%@",[personDictionary objectForKey:@"pageId"]];
        if (![tableId isEqualToString:@""]) {
            [parama setObject:@"2" forKey:@"tableId"];
        }
        if (![pageId isEqualToString:@""]) {
            [parama setObject:@"4713" forKey:@"pageId"];
        }
        
//        [parama setObject:@"2873" forKey:@"pageId"];
//        [parama setObject:@"2" forKey:@"tableId"];
    }else{
        NSDictionary *personDictionary = personDic();
        NSLog(@"%@",personDictionary);
        NSString *tableId = [NSString stringWithFormat:@"%@",[personDictionary objectForKey:@"tableId"]];
        NSString *pageId = [NSString stringWithFormat:@"%@",[personDictionary objectForKey:@"pageId"]];
        if (![tableId isEqualToString:@""]) {
            [parama setObject:tableId forKey:@"tableId"];
        }
        if (![pageId isEqualToString:@""]) {
            [parama setObject:pageId forKey:@"pageId"];
        }
    }
    

    requestUrl = [NSString stringWithFormat:@"%@%@", getBase(), PERSONAL_INFO_URl];
    [NetRequest requestUrl:requestUrl andParamas: parama andReturnBlock:^(NSData *data, NSError *error) {
        
        if(data!=nil&&!error){
            
            NSLog(@"%@",data);
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",str);
            NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", strData);
            
            NSLog(@"%@",obj);
            // 数据处理，当没问题的情况下
            [self dataUse:obj];
            
        }else{
            NSString *str = [NSString stringWithFormat:@"%@", error.localizedDescription];
            [self showLabelWithText:str];
            
        }
        
        
    }];
}

-(void)dataUse:(id)obj{
    dataArr = [[obj objectForKey:@"pageSet"]objectForKey:@"fieldSet"];
    NSArray *arr =[obj objectForKey:@"dataList"];

    
    if (arr.count>0) {
        dic =arr[0];
    }
    

    
     [self.tbView reloadData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataArr count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UILabel *la =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, KWIDTH-40, 400)];
    NSDictionary *ddd =[dataArr objectAtIndex:indexPath.row];
    NSString *right = [ddd objectForKey:@"fieldAliasName"];
    la.numberOfLines = 0;
    la.text = [dic objectForKey:right];
    CGSize sizec = [la boundingRectWithSize:CGSizeMake(KWIDTH-40, 0)];
    
    if(sizec.height<30){
        return 60;
    }else{
      return sizec.height+70;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"identifier";
    ListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if(cell==nil){
        cell = [[ListDetailCell alloc]initWithReuseIdentifier:str];
    }
    cell.userInteractionEnabled = NO;
     [cell setEditIs:NO];
    NSDictionary *ddd =[dataArr objectAtIndex:indexPath.row];
    NSString *right = [ddd objectForKey:@"fieldAliasName"];
    cell.title.text = [ddd objectForKey:@"fieldCnName"];
    cell.titleLabel.text = [dic objectForKey:right];
        return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

-(void)clearCacheAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
